export function getDate(type) {
  // 获取当前日期
  const timestamp = Date.parse(new Date())
  const date = new Date(timestamp)
  if (type === 'tomorrow') { // 明天
    date.setDate(date.getDate() + 1)
  } else if (type === 'today') { // 今天
    date.setDate(date.getDate())
  }
  // 获取年份
  var Y = date.getFullYear()
  // 获取月份
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  // 获取当日日期
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  const todayDate = Y + '-' + M + '-' + D
  return todayDate
}
export function getDateTime(type) {
  // 获取当前日期
  const timestamp = Date.parse(new Date())
  const date = new Date(timestamp)
  if (type === 'tomorrow') { // 明天
    date.setDate(date.getDate() + 1)
  } else if (type === 'today') { // 今天
    date.setDate(date.getDate())
  }
  // 获取年份
  var Y = date.getFullYear()
  // 获取月份
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  // 获取当日日期
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  const todayDate = Y + '-' + M + '-' + D + ' ' + H + ':' + m + ':' + s
  return todayDate
}
// 日期年月日的加减
// function setDateTest() {
// 	addOrReduceDate("D","2019-6-15 14:45:16","5");
// }
// type:年、月或者日
// data:日期
// 加减量
export function addOrReduceDate(type, date, num) {
  var nowDate = null
  var strDate = ''
  num = parseInt(num) // 防止传入字符串报错
  var seperator1 = '-'
  var seperator2 = ':'
  if (date === '') {
    nowDate = new Date()
  } else {
    nowDate = new Date(date)
  }

  if (type === 'Y') {
    nowDate.setFullYear(nowDate.getFullYear() + num)
  }
  if (type === 'M') {
    nowDate.setMonth(nowDate.getMonth() + num)
  }
  if (type === 'D') {
    nowDate.setDate(nowDate.getDate() + num)
  }
  if (type === 'A') {
    nowDate.setFullYear(nowDate.getFullYear() + num)
    nowDate.setMonth(nowDate.getMonth() + num)
    nowDate.setDate(nowDate.getDate() + num)
  }
  var year = nowDate.getFullYear() // 年
  var month = nowDate.getMonth() + 1 // 月
  strDate = nowDate.getDate() // 日
  var hours = nowDate.getHours() // 时
  var minutes = nowDate.getMinutes() // 分
  var seconds = nowDate.getSeconds() // 秒
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = '0' + seconds
  }
  var dateStr = year + seperator1 + month + seperator1 + strDate + ' ' + hours + seperator2 + minutes + seperator2 + seconds
  // console.log(dateStr)
  return dateStr
}

// 计算当前日期距离目标日期经过了多少天 传入参数是用 '-' 分割的字符串
export function calDate(date) {
  const lastDate = date.split('-')
  var sRDate = new Date(lastDate[0], lastDate[1] - 1, lastDate[2])
  // console.log(sRDate)
  const nRDate = new Date()
  var result = (nRDate - sRDate) / (24 * 60 * 60 * 1000)
  return Math.floor(result)
}
